/**最多保留小数点后两位 */
const fixto2 = price => {
  price = String(price)
  if (price.includes('.')) {
    const i = price.indexOf('.')
    const z = parseInt(price)
    const f = price.slice(i + 1)
    if (f.length <= 2) {
      return Number(price)
    } else {
      return Number(z + '.' + f.slice(0, 2))
    }
  } else {
    return Number(price)
  }
}

/*计算商品扣除税率后所得额*/
const actualGmoney = (price) => {
  if (typeof price !== 'number') {
    return 'price is not a number !'
  }
  if (price < 10) {
    return price
  } else if (price < 50) {
    return fixto2(price * 0.9)
  } else if (price < 150) {
    return fixto2(price * 0.85)
  } else if (price < 500) {
    return fixto2(price * 0.8)
  } else {
    return fixto2(price * 0.7)
  }
}

module.exports = actualGmoney